package com.sd.repay.service.impl;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.service.MerFeeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sd
 * @Description: 商户费率信息服务类
 * @date 2017年10月25日
 */
@Service
public class MerFeeServiceImpl implements MerFeeService {

    private static Logger log = LoggerFactory.getLogger(RepayPlanServiceImpl.class);

    @Autowired
    private BaseDao baseDao;

    /**
     * 新增商户费率信息
     *
     * @param merFeeParams
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int saveMerFeeInfo(Map<String, String> merFeeParams) throws Exception {

        String sql = " INSERT INTO t_mer_fee(oem_type,mer_type,mer_no,quick_fee_rate,quick_single_fee,perfect_fee_rate,perfect_single_fee,withdraw_fee_rate,withdraw_single_fee,account_ratio,create_time) " +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(merFeeParams.get("oemType"));
        params.add(merFeeParams.get("merType"));
        params.add(merFeeParams.get("merNo"));
        params.add(merFeeParams.get("quickFeeRate"));
        params.add(merFeeParams.get("quickSingleFee"));
        params.add(merFeeParams.get("perfectFeeRate"));
        params.add(merFeeParams.get("perfectSingleFee"));
        params.add(merFeeParams.get("withdrawFeeRate"));
        params.add(merFeeParams.get("withdrawSingleFee"));
        params.add(StringUtils.isBlank(merFeeParams.get("accountRatio")) ? "100" : merFeeParams.get("accountRatio"));
        params.add(new Date());
        return baseDao.execute(sql, params.toArray());
    }

    @Override
    @Transactional
    public int updateMerFeeInfo(Map<String, String> merFeeParams) throws Exception {
        String sql = " UPDATE t_mer_fee SET quick_fee_rate = ?, quick_single_fee = ?, perfect_fee_rate = ?, perfect_single_fee = ?, " +
                " withdraw_fee_rate = ?, withdraw_single_fee = ? WHERE mer_type = ? AND mer_no = ? ";
        List<Object> params = new ArrayList<>();
        params.add(merFeeParams.get("quickFeeRate"));
        params.add(merFeeParams.get("quickSingleFee"));
        params.add(merFeeParams.get("perfectFeeRate"));
        params.add(merFeeParams.get("perfectSingleFee"));
        params.add(merFeeParams.get("withdrawFeeRate"));
        params.add(merFeeParams.get("withdrawSingleFee"));
        params.add(merFeeParams.get("merType"));
        params.add(merFeeParams.get("merNo"));
        return baseDao.execute(sql, params.toArray());
    }

    /**
     * 获取商户费率信息
     *
     * @param merType
     * @param merNo
     * @param oemType
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getMerFeeByMerNoAndOemType(String merType, String merNo, String oemType) throws Exception {

        String sql = " SELECT * FROM t_mer_fee WHERE oem_type = ? AND mer_type = ? AND mer_no = ? ";
        return baseDao.findFirst(sql, new Object[]{oemType, merType, merNo});
    }
}